package com.tbq.game.service.activity.start

import org.w3c.dom.Element
import org.w3c.dom.Node
import java.io.InputStream
import java.util.HashMap
import javax.xml.parsers.DocumentBuilderFactory

object ParseUpdateXml {
    fun parseXml(inStream: InputStream): HashMap<String, String> {
        val hashMap = HashMap<String, String>()
        // 实例化一个文档构建器工厂
        val factory = DocumentBuilderFactory.newInstance()
        // 通过文档构建器工厂获取一个文档构建器
        val builder = factory.newDocumentBuilder()
        // 通过文档通过文档构建器构建一个文档实例
        val document = builder.parse(inStream)
        //获取XML文件根节点
        val root = document.documentElement
        //获得所有子节点
        val childNodes = root.childNodes
        for (j in 0 until childNodes.length) {
            //遍历子节点
            val childNode = childNodes.item(j)
            if (childNode.nodeType == Node.ELEMENT_NODE) {
                val childElement = childNode as Element
                //版本号
                if ("version" == childElement.nodeName) {
                    hashMap.put("version", childElement.firstChild.nodeValue)
                } else if ("name" == childElement.nodeName) {
                    hashMap.put("name", childElement.firstChild.nodeValue)
                } else if ("url" == childElement.nodeName) {
                    hashMap.put("url", childElement.firstChild.nodeValue)
                } else if ("content" == childElement.nodeName) {
                    hashMap.put("content", childElement.firstChild.nodeValue)
                }//下载地址
                //软件名称
            }
        }
        return hashMap
    }
}